/*
 * Copyright 2010, 2011  Service de logiciel et developpement R.J. Potter (Robert J Potter)
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectCardImpl.java
 *
 * Created on 30-Jan-2011, 5:10:31 PM
 */

package com.sldrjp.wonderland.modules.cardwall.client;

import com.sldrjp.wonderland.modules.cardwall.common.cell.CardWallCardCellClientState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author Bob
 */
public class SelectCardImpl extends javax.swing.JPanel implements SelectCard {

    CardWallManager manager;

    /**
     * Creates new form SelectCardImpl
     */
    public SelectCardImpl(CardWallManager manager) {
        initComponents();
        this.manager = manager;
    }

    public Component getAsComponent() {
        return this;
    }

    private int sectionNumber;


    public void setSectionNumber(int sectionNumber) {
        this.sectionNumber = sectionNumber;
    }


    public void removeFromSelectable(CardWallCardCellClientState state) {
        String uniqueIDToFind = state.getUniqueID();
        for (int i = 0; i < comboBox.getItemCount(); i++) {
            CardStateWrapper wrapper = (CardStateWrapper) comboBox.getItemAt(i);
            if (wrapper.getUniqueID().equals(uniqueIDToFind)) {
                comboBox.removeItem(wrapper);
                return;
            }
        }

    }


    public void newCard(CardWallCardCellClientState state) {
        comboBox.addItem(new CardStateWrapper(state));
        this.validate();
        this.repaint();
    }


    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(200, 33));

        button = new JButton("+");
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setFont(button.getFont().deriveFont(10f));
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });
        comboBox = new JComboBox();
        comboBox.setFont(button.getFont().deriveFont(10f));
        comboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(button)
                                .addGap(3, 3, 3))

        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }

    private void buttonActionPerformed(ActionEvent evt) {
        CardStateWrapper selectedItem = (CardStateWrapper) comboBox.getSelectedItem();
        if (selectedItem != null) {
            manager.requestRestoreCard(sectionNumber, selectedItem.getState());
        }
    }

    private void comboActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private JComboBox comboBox;
    private JButton button;


}
